import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:chat_shop/common/appbar/appbar.dart';
import 'package:chat_shop/common/const/cosnt.dart';
import 'package:zevent_bus/zevent_bus.dart';

class MsgPage extends StatefulWidget {
  @override
  State<MsgPage> createState() => _MsgPageState();
}

class _MsgPageState extends State<MsgPage> with AutomaticKeepAliveClientMixin {
  List<dynamic> data = [];
  VoidCallback? listener;
  num money = 0;
  bool isSelectAll = false;
  late VoidCallback? _remove;
  @override
  void initState() {
    super.initState();
    _add();
  }

  void _add() {
    _remove = EventBus.instance.addListener(
      eventKey: 'chat',
      callback: (arg) {
        setState(() {
          data.add(arg);
        });
      },
    );
  }

  @override
  void dispose() {
    _remove?.call();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: CustomAppbar.normal(
        title: "消息中心",
        leading: const SizedBox(),
      ),
      body: ListView.separated(
        padding: const EdgeInsets.all(12),
        itemBuilder: (context, index) {
          if (data.isEmpty)
            return Column(
              children: [
                const SizedBox(
                  height: 80,
                ),
                Icon(
                  Icons.landscape,
                  color: Colors.grey[400],
                  size: 80,
                ),
                const SizedBox(
                  height: 12,
                ),
                Text(
                  "暂无消息",
                  style: TextStyle(color: Colors.grey),
                )
              ],
            );
          return _Cell(
            data: data[index],
          );
        },
        separatorBuilder: (_, __) => Divider(),
        itemCount: data.isEmpty ? 1 : data.length,
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class _Cell extends StatelessWidget {
  final Map<String, dynamic> data;
  const _Cell({Key? key, required this.data}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 80.0,
      color: Colors.white,
      child: InkWell(
        onTap: () {
          Navigator.pushNamed(context, "/msg", arguments: {"model": data});
        },
        child: Row(
          children: [
            Container(
              width: 36,
              height: 36,
              margin: const EdgeInsets.only(right: 12),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(5),
                child: CachedNetworkImage(
                  fit: BoxFit.fill,
                  imageUrl: data['download_url'],
                ),
              ),
            ),
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  new Text(
                    data['author'],
                    style: TextStyle(fontSize: 18, height: 28 / 18, fontWeight: FontWeight.bold),
                  ),
                  const SizedBox(height: 4),
                  Text("你要过来么?"),
                ],
              ),
            ),
            Container(
              width: 15,
              height: 15,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.redAccent,
              ),
              alignment: Alignment.center,
              child: Text(
                data['id'],
                style: TextStyle(color: Colors.white),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
